﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;

namespace TechControl
{
    class GeraTermos
    {
        private static GeraTermos instance;

        private GeraTermos() { }

        public static GeraTermos getInstance()
        {
            if (instance == null)
            {
                instance = new GeraTermos();
            }
            return instance;
        }

        //preenche o termo de devolução.
        public void preencherDevolucao(string docMatriz, string nome, string rg, string date, string equipamento, string marca, string modelo,
            string patrimonio, string empresa, string serial, string imei, string dia, string ano)
        {
            string diaS = diaDaSemana();
            string mes = mesAtual();

            //Objeto a ser usado nos parâmetros opcionais
            object missing = System.Reflection.Missing.Value;

            //Abre a aplicação Word e faz uma cópia do documento mapeadoWord.
            Word.Application oApp = new Word.Application();

            object template = docMatriz;

            Word.Document oDoc = oApp.Documents.Add(ref template, ref missing, ref missing, ref missing);

            //Troca o conteúdo de alguns tags
            Word.Range oRng = oDoc.Range(ref missing, ref missing);
            object FindText = "[NOME]";
            object ReplaceWith = nome.ToUpper();
            object MatchWholeWord = true;
            object Forward = false;
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);

            //teste
            FindText = "[RG]";
            ReplaceWith = rg.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[DATE]";
            ReplaceWith = date.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[EQUIPAMENTO]";
            ReplaceWith = equipamento.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[MARCA]";
            ReplaceWith = marca.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[MODELO]";
            ReplaceWith = modelo.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[PATRIMONIO]";
            ReplaceWith = patrimonio.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[EMPRESA]";
            ReplaceWith = empresa.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[SERIAL]";
            ReplaceWith = serial.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[IMEI]";
            ReplaceWith = imei.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[DIA DA SEMANA]";
            ReplaceWith = diaS.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[DIA]";
            ReplaceWith = dia;
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[MES]";
            ReplaceWith = mes;
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[ANO]";
            ReplaceWith = ano;
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[NOME1]";
            ReplaceWith = nome.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);


            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oApp.Visible = true;
        }

        //preenche o termo de responsabilidade.
        public void preencherResponsabilidade(string docMatriz, string nome, string rg, string date, string equipamento, string marca, string modelo, 
            string patrimonio, string empresa, string serial, string imei, string dia, string ano)
        {
            string diaS = diaDaSemana();
            string mes = mesAtual();

            //Objeto a ser usado nos parâmetros opcionais
            object missing = System.Reflection.Missing.Value;

            //Abre a aplicação Word e faz uma cópia do documento mapeadoWord.
            Word.Application oApp = new Word.Application();

            object template = docMatriz;

            Word.Document oDoc = oApp.Documents.Add(ref template, ref missing, ref missing, ref missing);

            //Troca o conteúdo de alguns tags
            Word.Range oRng = oDoc.Range(ref missing, ref missing);
            object FindText = "[NOME]";
            object ReplaceWith = nome.ToUpper();
            object MatchWholeWord = true;
            object Forward = false;
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);

            //teste
            FindText = "[RG]";
            ReplaceWith = rg.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[DATE]";
            ReplaceWith = date.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[EQUIPAMENTO]";
            ReplaceWith = equipamento.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[MARCA]";
            ReplaceWith = marca.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[MODELO]";
            ReplaceWith = modelo.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[PATRIMONIO]";
            ReplaceWith = patrimonio.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[EMPRESA]";
            ReplaceWith = empresa.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[SERIAL]";
            ReplaceWith = serial.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[IMEI]";
            ReplaceWith = imei.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[DIA DA SEMANA]";
            ReplaceWith = diaS.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[DIA]";
            ReplaceWith = dia;
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[MES]";
            ReplaceWith = mes;
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[ANO]";
            ReplaceWith = ano;
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);
            FindText = "[NOME1]";
            ReplaceWith = nome.ToUpper();
            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oRng = oDoc.Range(ref missing, ref missing);


            oRng.Find.Execute(ref FindText, ref missing, ref MatchWholeWord, ref missing, ref missing, ref missing, ref Forward, ref missing, ref missing, ref ReplaceWith, ref missing, ref missing, ref missing, ref missing, ref missing);
            oApp.Visible = true;
        }

        //converte o dia da semana.
        public string diaDaSemana()
        {
            string dia = "";
            string day = DateTime.Now.DayOfWeek.ToString();
            try
            {
                if (day == "Sunday")
                    dia = "DOMINGO";
                else if (day == "Monday")
                    dia = "SEGUNDA-FEIRA";
                else if (day == "Tuesday")
                    dia = "TERÇA-FEIRA";
                else if (day == "Wednesday")
                    dia = "QUARTA-FEIRA";
                else if (day == "Thursday")
                    dia = "QUINTA-FEIRA";
                else if (day == "Friday")
                    dia = "SEXTA-FEIRA";
                else if (day == "Saturday")
                    dia = "SABADO";
            }
            finally
            {
                
            }
            return dia;
        }

        //converte o mes.
        public string mesAtual()
        {
            string mes = "";
            string month = DateTime.Now.Month.ToString();
            try
            {
                if (month == "1")
                    mes = "JANEIRO";
                else if (month == "2")
                    mes = "FEVEREIRO";
                else if (month == "3")
                    mes = "MARÇO";
                else if (month == "4")
                    mes = "ABRIL";
                else if (month == "5")
                    mes = "MAIO";
                else if (month == "6")
                    mes = "JUNHO";
                else if (month == "7")
                    mes = "JULHO";
                else if (month == "8")
                    mes = "AGOSTO";
                else if (month == "9")
                    mes = "SETEMBRO";
                else if (month == "10")
                    mes = "OUTUBRO";
                else if (month == "11")
                    mes = "NOVEMBRO";
                else if (month == "12")
                    mes = "DEZEMBRO";    
            }
            finally
            {

            }
            return mes;
        }
    }
}
